package com.tang.kite.benchmark;

import com.tang.kite.benchmark.kite.KiteTester;
import com.tang.kite.benchmark.mybatis.MyBatisTester;
import org.apache.ibatis.logging.LogFactory;

import java.util.function.Consumer;

/**
 * @author Tang
 */
public class Main {

    public static void main(String[] args) {
        var dataSource = new BaseBenchmark().getDataSource();
        var kiteTester = new KiteTester(dataSource);
        var myBatisTester = new MyBatisTester(dataSource);
        LogFactory.useNoLogging();

        var myBatisTime = measureTimeMillis(() -> repeat(1000, i -> myBatisTester.select()));
        var kiteTime = measureTimeMillis(() -> repeat(1000, i -> kiteTester.select()));
        System.out.println("Kite: " + kiteTime + " ms");
        System.out.println("MyBatis: " + myBatisTime + " ms");
    }

    public static long measureTimeMillis(Runnable block) {
        long start = System.currentTimeMillis();
        block.run();
        return System.currentTimeMillis() - start;
    }

    public static void repeat(int times, Consumer<Integer> action) {
        if (times < 0) {
            throw new IllegalArgumentException("Times must be non-negative.");
        }

        for (int index = 0; index < times; index++) {
            action.accept(index);
        }
    }

}
